package com.cbs.android.function.capture;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.Gravity;
import android.widget.Toast;

import com.cbs.android.component.application.activity.CBSActivity;
import com.cbs.android.component.application.activity.handler.CBSActivityResultHandler;

import java.io.File;
import java.lang.ref.WeakReference;

/**
 * Created by broche on 08/11/2016.
 */

public class CaptureFunction {
    private static final int REQUEST_CAPTURE = 0xFFEA;
    private static final String tmpFileName = "cbs_capture";
    private static ToastHandler toastHandler = null;

    public static void startImageCapture(CBSActivity activity, CaptureHandler handler) {
        activity.addActivityResultHandler(new MyActivityResultHandler(activity, handler));

        File outFile = new File(activity.getExternalCacheDir(), tmpFileName);
        if (!outFile.getParentFile().exists()) {
            outFile.getParentFile().mkdirs();
        }

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra("return-data", false);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(outFile));
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        activity.startActivityForResult(intent, REQUEST_CAPTURE);
    }

    public static void startVideoCapture(CBSActivity activity, int duration, String hint, CaptureHandler handler) {
        activity.addActivityResultHandler(new MyActivityResultHandler(activity, handler));

        File outFile = new File(activity.getExternalCacheDir(), tmpFileName);
        if (!outFile.getParentFile().exists()) {
            outFile.getParentFile().mkdirs();
        }

        if (toastHandler != null) {
            toastHandler.reset();
        }
        if (hint != null && !hint.equals("")) {
            toastHandler = new ToastHandler(activity, hint);
            toastHandler.obtainMessage(0).sendToTarget();
        }

        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        intent.putExtra("return-data", false);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(outFile));
        intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, duration);
        activity.startActivityForResult(intent, REQUEST_CAPTURE);
    }

    public static void startVideoCapture(CBSActivity activity, CaptureHandler handler) {
        startVideoCapture(activity, 0, null, handler);
    }

    private static class MyActivityResultHandler implements CBSActivityResultHandler {
        private final WeakReference<CBSActivity> activity;
        private final CBSActivityResultHandler handler;
        private final File outputFile;

        public MyActivityResultHandler(CBSActivity activity, CBSActivityResultHandler handler) {
            this.activity = new WeakReference<>(activity);
            this.handler = handler;
            this.outputFile = new File(activity.getExternalCacheDir(), tmpFileName);
        }

        @Override
        public void onActivityResult(int requestCode, int responseCode, Intent intent) {
            if (requestCode == REQUEST_CAPTURE) {
                if (handler != null) {
                    Intent intentResult = new Intent();
                    intentResult.setData(Uri.fromFile(outputFile));
                    handler.onActivityResult(requestCode, responseCode, intentResult);
                }
                if (activity != null && activity.get() != null) {
                    activity.get().removeActivityResultHandler(this);
                }
                if (toastHandler != null) {
                    toastHandler.reset();
                    toastHandler = null;
                }
            }
        }
    }

    private static class ToastHandler extends Handler {
        private Toast toast = null;

        public ToastHandler(Context context, String content) {
            toast = Toast.makeText(context, content, Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }

        @Override
        public void handleMessage(Message msg) {
            if (toast != null) {
                toast.show();
                sendEmptyMessageDelayed(0, 1000);
            }
        }

        public void reset() {
            removeMessages(0);
            if (toast != null) {
                toast.cancel();
            }
        }
    }
}
